package com.qf.index.gcrdo;


import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.qf.index.gcrdo.httpclient.SmallTool;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;


@Slf4j
public class CheckPathyWayParse {

    public static void main(String[] args) throws Exception {


        // 校验通路 通过网址 kegg
        // 药物，数字代号：1; 基因，数字代号：2; 通路，数字代号：3; 疾病，数字代号：4
        Db use = Db.use(DSFactory.get("localdb"));
        List<Entity> query = use.query("select * from word_and_code_set_map_new where type = '通路' and tongyi_back = '不是' and pathway is null ");
        SmallTool.printMessError("查询到的数量："+query.size());
        for (Entity entity : query) {
            String check3 = entity.getStr("check3");
            String word = entity.getStr("word");
            if (StringUtil.isNotNull(check3)) {
                Document parse = Jsoup.parse(check3);
                Elements table = parse.select("table");
                if (table.size() == 1) {
                    entity.set("pathway", "不是");
                }else {
                    Element element = table.get(1);
                    Elements tr = element.select("tr");
                    tr.remove(0);
                    boolean isMath = false;
                    for (Element element1 : tr) {
                        Element element2 = element1.select("td").get(1);
                        String text = element2.text();
                        if (StringUtil.isNotNull(text)) {
                            // 比较word 和 text 是不是一样 排除掉大小写空格只保留英文和数字
                            String word1 = StringUtil.removeNonAlphanumeric(word).toLowerCase();
                            String text1 = StringUtil.removeNonAlphanumeric(text).toLowerCase();
                            if (word1.equalsIgnoreCase(text1)) {
                                isMath = true;
                                System.out.println("匹配到了可以");
                                entity.set("pathway", "是");
                                break;
                            }
                        }
                    }
                    if (!isMath) {
                        entity.set("pathway", "不是");
                    }

                }
                use.insertOrUpdate(entity, "id");
            }
        }
    }


}


